package org.sp.controller;

import org.sp.entity.User;
import org.sp.service.ITestUserService;
import org.sp.service.impl.TestUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddUserController" ,urlPatterns = "/user/add")
public class AddUserController extends HttpServlet {
    private ITestUserService service = new TestUserServiceImpl();

    /**
     * 新增
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");
        User user = new User(null,name,mobile,address);
        Boolean flag = service.addUser(user);
        if (flag){
            request.getSession().setAttribute("msg","添加成功");
        }else {
            request.getSession().setAttribute("msg","添加失败");
        }
        response.sendRedirect(request.getContextPath()+"/");
    }
}
